package nl.boelen.game.handler;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyboardHandler implements KeyListener {

    public KeyboardHandler() {
    }

    public class Key {
        private boolean pressed = false;

        public boolean isPressed() {
            return pressed;
        }

        public void toggle(boolean isPressed) {
            pressed = isPressed;
        }
    }

    public Key up = new Key();
    public Key down = new Key();
    public Key left = new Key();
    public Key right = new Key();
    public Key x = new Key();

    @Override
    public void keyPressed(KeyEvent e) {
        toggleKey(e.getKeyCode(), true);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        toggleKey(e.getKeyCode(), false);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    public void toggleKey(int keyCode, boolean isPressed) {
        switch (keyCode) {
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                up.toggle(isPressed);
                break;
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                down.toggle(isPressed);
                break;
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                left.toggle(isPressed);
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                right.toggle(isPressed);
                break;
            case KeyEvent.VK_X:
                x.toggle(isPressed);
            default:
                break;
        }
    }
}
